import axios from "axios";
import Qs from "qs"

const order={
  namespaced: true,
  state:()=>{
    return{

    }
  },
  mutations:{

  },
  actions:{
    getOrderUnpaid({ state, commit, rootState }){
      /**
       *  查询所有待支付订单
       */
      return new Promise((resolve, reject) => {
        axios.get('/order/orderlist/unpaid').then(res=>{
          resolve(res.data);
        }).catch(e=>{
          reject(e);
        })
      })
    },
    getOrderPaid({ state, commit, rootState }){
      /**
       *  查询所有已支付订单
       */
      return new Promise((resolve, reject) => {
        axios.get('/order/orderlist/paid').then(res=>{
          resolve(res.data);
        }).catch(e=>{
          reject(e);
        })
      })

    },
    getOrderDelivery({ state, commit, rootState }){
      /**
       *  查询所有派送中订单
       */
      return new Promise((resolve, reject) => {
        axios.get('/order/orderlist/delivery').then(res=>{
          resolve(res.data);
        }).catch(e=>{
          reject(e);
        })
      })

    },
    getOrderFinished({ state, commit, rootState }){
      /**
       *  查询所有已完成订单
       */
      return new Promise((resolve, reject) => {
        axios.get('/order/orderlist/finished').then(res=>{
          resolve(res.data);
        }).catch(e=>{
          reject(e);
        })
      })
    },
    getOrder({ state, commit, rootState },sub_order_id){
      /**
       *  查询单独订单，需要子订单号
       */
      console.log(`查询订单：${sub_order_id}`);
      console.log(Qs.stringify({sub_order_id: sub_order_id}));
      return new Promise((resolve, reject) => {
        axios.post('/order/queryorder',
          Qs.stringify({sub_order_id:sub_order_id}),
          {
            headers: {
              'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
            }
          }
        ).then(res=>{
          console.log(res.data);
          resolve(res.data);
        }).catch(e=>{
          reject(e);
        })
      })
    },
    payOrder({ state, commit, rootState },sub_order_id_list){
      /**
       *  （待支付订单）支付订单，传入一个数组
       */
      return new Promise((resolve, reject) => {
        axios.post('/order/payorder',
          Qs.stringify({sub_order_id:sub_order_id_list},{ arrayFormat: 'repeat' }),
          {
            headers: {
              'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
            }
          }
        ).then(res=>{
          resolve();
        }).catch(e=>{
          reject(e);
        })
      })
    },
    closeOrder({ state, commit, rootState },sub_order_id_list){
      /**
       *  （待支付订单）关闭订单，传入一个数组
       */
      console.log(sub_order_id_list);
      console.log(Qs.stringify({sub_order_id_list:sub_order_id_list},{ arrayFormat: 'repeat' }), );
      return new Promise((resolve, reject) => {
        axios.post('/order/closeorder',
          Qs.stringify({sub_order_id_list: sub_order_id_list},{arrayFormat:'repeat'}),
          {
            headers: {
              'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
            }
          }
        ).then(res=>{
          resolve();
        }).catch(e=>{
          reject(e);
        })
      })
    },
    deliverOrder({ state, commit, rootState },sub_order_id_list){
      /**
       *  报告已收货，传入一个数组
       */
      return new Promise((resolve, reject) => {
        let temp=Qs.stringify({sub_order_id_list:sub_order_id_list},{arrayFormat:'repeat'})
        console.log(temp);
        axios.post('/order/delivered',
          temp,
        {
          headers: {
            'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
          }
        }
        ).then(res=>{
          resolve();
        }).catch(e=>{
          reject(e);
        })
      })
    },

  }
}

export default order;
